
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>异常 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="事件循环" href="asyncio-eventloop.html" />
    <link rel="prev" title="队列集" href="asyncio-queue.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/asyncio-exceptions.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="asyncio-eventloop.html" title="事件循环"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="asyncio-queue.html" title="队列集"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> --- 异步 I/O</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="exceptions">
<span id="asyncio-exceptions"></span><h1>异常<a class="headerlink" href="#exceptions" title="永久链接至标题">¶</a></h1>
<dl class="exception">
<dt id="asyncio.TimeoutError">
<em class="property">exception </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">TimeoutError</code><a class="headerlink" href="#asyncio.TimeoutError" title="永久链接至目标">¶</a></dt>
<dd><p>该操作已超过规定的截止日期。</p>
<div class="admonition important">
<p class="admonition-title">重要</p>
<p>这个异常与内置 <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a> 异常不同。</p>
</div>
</dd></dl>

<dl class="exception">
<dt id="asyncio.CancelledError">
<em class="property">exception </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">CancelledError</code><a class="headerlink" href="#asyncio.CancelledError" title="永久链接至目标">¶</a></dt>
<dd><p>该操作已被取消。</p>
<p>取消asyncio任务时，可以捕获此异常以执行自定义操作。在几乎所有情况下，都必须重新引发异常。</p>
<div class="admonition important">
<p class="admonition-title">重要</p>
<p>此异常是 <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> 的子类，因此可能会被一个过于宽泛的 <code class="docutils literal notranslate"><span class="pre">try..except</span></code> 块意外地阻塞：</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
    <span class="k">await</span> <span class="n">operation</span>
<span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
    <span class="c1"># The cancellation is broken because the *except* block</span>
    <span class="c1"># suppresses the CancelledError exception.</span>
    <span class="n">log</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">&#39;an error has occurred&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>相反，应使用以下模式:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
    <span class="k">await</span> <span class="n">operation</span>
<span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">CancelledError</span><span class="p">:</span>
    <span class="k">raise</span>
<span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
    <span class="n">log</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">&#39;an error has occurred&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</dd></dl>

<dl class="exception">
<dt id="asyncio.InvalidStateError">
<em class="property">exception </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">InvalidStateError</code><a class="headerlink" href="#asyncio.InvalidStateError" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a> 或 <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> 的内部状态无效。</p>
<p>在为已设置结果值的未来对象设置结果值等情况下，可以引发此问题。</p>
</dd></dl>

<dl class="exception">
<dt id="asyncio.SendfileNotAvailableError">
<em class="property">exception </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">SendfileNotAvailableError</code><a class="headerlink" href="#asyncio.SendfileNotAvailableError" title="永久链接至目标">¶</a></dt>
<dd><p>&quot;sendfile&quot; 系统调用不适用于给定的套接字或文件类型。</p>
<p>子类 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> 。</p>
</dd></dl>

<dl class="exception">
<dt id="asyncio.IncompleteReadError">
<em class="property">exception </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">IncompleteReadError</code><a class="headerlink" href="#asyncio.IncompleteReadError" title="永久链接至目标">¶</a></dt>
<dd><p>请求的读取操作未完全完成。</p>
<p>由 <a class="reference internal" href="asyncio-stream.html#asyncio-streams"><span class="std std-ref">asyncio stream APIs</span></a> 提出</p>
<p>此异常是 <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> 的子类。</p>
<dl class="attribute">
<dt id="asyncio.IncompleteReadError.expected">
<code class="sig-name descname">expected</code><a class="headerlink" href="#asyncio.IncompleteReadError.expected" title="永久链接至目标">¶</a></dt>
<dd><p>预期字节的总数（ <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> ）。</p>
</dd></dl>

<dl class="attribute">
<dt id="asyncio.IncompleteReadError.partial">
<code class="sig-name descname">partial</code><a class="headerlink" href="#asyncio.IncompleteReadError.partial" title="永久链接至目标">¶</a></dt>
<dd><p>到达流结束之前读取的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 字符串。</p>
</dd></dl>

</dd></dl>

<dl class="exception">
<dt id="asyncio.LimitOverrunError">
<em class="property">exception </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">LimitOverrunError</code><a class="headerlink" href="#asyncio.LimitOverrunError" title="永久链接至目标">¶</a></dt>
<dd><p>在查找分隔符时达到缓冲区大小限制。</p>
<p>由 <a class="reference internal" href="asyncio-stream.html#asyncio-streams"><span class="std std-ref">asyncio stream APIs</span></a> 提出</p>
<dl class="attribute">
<dt id="asyncio.LimitOverrunError.consumed">
<code class="sig-name descname">consumed</code><a class="headerlink" href="#asyncio.LimitOverrunError.consumed" title="永久链接至目标">¶</a></dt>
<dd><p>要消耗的字节总数。</p>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="asyncio-queue.html"
                        title="上一章">队列集</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="asyncio-eventloop.html"
                        title="下一章">事件循环</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/asyncio-exceptions.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="asyncio-eventloop.html" title="事件循环"
             >下一页</a> |</li>
        <li class="right" >
          <a href="asyncio-queue.html" title="队列集"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="asyncio.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> --- 异步 I/O</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>